# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.20.1
pkgrel=1
pkgdesc="OpenAL audio library for use with opengl (mingw-w64)"
arch=('any')
url="https://github.com/kcat/openal-soft"
license=('GPL2')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-fluidsynth"
             "${MINGW_PACKAGE_PREFIX}-portaudio")
options=('strip' 'staticlibs')
source=(https://github.com/kcat/openal-soft/archive/openal-soft-${pkgver}.tar.gz
        0001-versioned-w32-dll.mingw.patch
        0002-w32ize-portaudio-loading.mingw.patch
        0003-openal-not-32.mingw.patch
        0004-disable-OSS-windows.patch
        0005-mingw-dont-check-libm.patch
        0006-mingw-use-own-import-library-winmm.patch)
sha256sums=('c32d10473457a8b545aab50070fe84be2b5b041e1f2099012777ee6be0057c13'
            'a594fade14f742ad203ec7c995e9e4772e15f133067c2d37cc9477b8c9787d5c'
            'f0bde7d3a8087530ab1bb2f48e59eec6f4cf60d59c5afe3a0c28c95e6b138751'
            '93894676b54415ce9d5c8c75eea28730845ecacfd0726bede2f459ed136da080'
            'aa6fc2efb76424d0337d609f3002abb8ff179ed15ae633c53433bcb176e643ba'
            'd25ea2d6d7ee3995a0648f725edd1bfde71bb1a07c91886a1572390ee03f3d00'
            '099f112cfa188f84a60570fdb9146baa6966e15b8739db83e605050acdc8cb4d')

prepare() {
  cd openal-soft-openal-soft-${pkgver}
  patch -p1 -i ${srcdir}/0001-versioned-w32-dll.mingw.patch
  patch -p1 -i ${srcdir}/0002-w32ize-portaudio-loading.mingw.patch
  patch -p1 -i ${srcdir}/0003-openal-not-32.mingw.patch
  patch -p1 -i ${srcdir}/0004-disable-OSS-windows.patch
  patch -p1 -i ${srcdir}/0005-mingw-dont-check-libm.patch
  patch -p1 -i ${srcdir}/0006-mingw-use-own-import-library-winmm.patch
}

build() {
  [[ -d "${srcdir}"/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p "${srcdir}/shared-${MINGW_CHOST}" && cd "${srcdir}/shared-${MINGW_CHOST}"

  DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DALSOFT_EXAMPLES=OFF \
    -DALSOFT_UTILS=ON \
    -DALSOFT_NO_CONFIG_UTIL=ON \
    -DLIBTYPE=SHARED \
    -DDSOUND_INCLUDE_DIR=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    -DDSOUND_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libdsound.a \
    ../openal-soft-openal-soft-${pkgver}
  make

  [[ -d "${srcdir}"/static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/static-${MINGW_CHOST}
  mkdir -p "${srcdir}/static-${MINGW_CHOST}" && cd "${srcdir}/static-${MINGW_CHOST}"
  
  # enabling utilities on static causes an undefined reference error.
  DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DALSOFT_EXAMPLES=OFF \
    -DALSOFT_UTILS=OFF \
    -DALSOFT_NO_CONFIG_UTIL=ON \
    -DLIBTYPE=STATIC \
    -DDSOUND_INCLUDE_DIR=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    -DDSOUND_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libdsound.a \
    ../openal-soft-openal-soft-${pkgver}
  make
}

package() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
